package com.jinyi.wwdeng.ui.steps.adapter

import android.graphics.Typeface
import android.text.Editable
import android.text.SpannableString
import android.text.Spanned
import android.text.TextWatcher
import android.text.style.ForegroundColorSpan
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.jinyi.base.utils.log.HHLog
import com.jinyi.lib_baseadapter.BGARecyclerViewAdapter
import com.jinyi.lib_baseadapter.BGAViewHolderHelper
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/18 10:43
 * @Annotation：流程常规通用适配器
 */
class StepCommonAdapter(recyclerView: RecyclerView?) : BGARecyclerViewAdapter<JSONObject>(recyclerView) {

    //是否是通用样式，通用样式 tvTitle为加粗字体，非通用样式为默认字体
    var typeFaceStyle = Typeface.BOLD

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        when(model.optString("action")){
            "DATE_PICK" ->{//时间选择
                return R.layout.item_step_common_select
            }
            "SELECT" -> {//选择
                return R.layout.item_step_common_select
            }
            else -> {//输入框
                return R.layout.item_step_common_input
            }
        }
    }

    override fun setItemChildListener(helper: BGAViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.selectLayout)//选择
    }

    override fun fillData(helper: BGAViewHolderHelper, position: Int, model: JSONObject) {
        //名称
        val tvTitle = helper.getTextView(R.id.tvTitle)
        tvTitle.text = findKeywordColor(ContextCompat.getColor(mContext,R.color.color_ff4d3e),model.optString("title"),"*")
        tvTitle.setTypeface(Typeface.DEFAULT,typeFaceStyle)

        when(model.optString("action")){
            "DATE_PICK",
            "SELECT" ->{//时间选择、选择
                showSelectView(helper,position,model)
            }
            else -> {//输入框
                showInputView(helper,position,model)
            }
        }
    }

    /**
     * 输入框类型
     */
    private fun showInputView(helper: BGAViewHolderHelper, position: Int, model: JSONObject){
        //提示语
        val hint = model.optString("hint")
        val editText = helper.getView<EditText>(R.id.editText)
        editText.setHint(hint)
        var showText = ""
        if (model.has("selectItem")){
            showText = model.optJSONObject("selectItem").optString("title")
        }
        editText.setText(showText)
        editText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE){
                editText.clearFocus()
                true
            }
            false
        }
        editText.setOnFocusChangeListener { v, hasFocus ->
            HHLog.e(">>>>>>是否失去焦点>>>>>>$hasFocus>>>>>>>${editText.text}")
            if (!hasFocus){
                TrackEventUtils.logEvent(String.format(TrackEventType.FORM.FORM_STEP_INPUT_XX,model.optString("paramName")),editText.text.toString())
            }
        }

        editText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
            override fun afterTextChanged(s: Editable?) {
                val obj = JSONObject()
                obj.put("title",s.toString())
                obj.put("value",s.toString())
                model.put("selectItem",obj)
            }
        })
    }

    /**
     * 选择类型
     */
    private fun showSelectView(helper: BGAViewHolderHelper, position: Int, model: JSONObject){
        //提示语
        val hint = model.optString("hint")
        val tvValue = helper.getTextView(R.id.tvValue)
        tvValue.setHint(hint)
        if (model.has("selectItem")){
            tvValue.text = model.getJSONObject("selectItem").optString("title")
        }
    }

    private fun findKeywordColor(color: Int, text: String, key: String): SpannableString {
        val spString = SpannableString(text)
        if (!text.contains(key)){
            return spString
        }
        val start = text.indexOf(key)
        val end = start + key.length
        spString.setSpan(ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return spString
    }
}